package com.jiayou.im.controller

import cn.hutool.system.UserInfo
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.jiayou.im.entity.Relation
import com.jiayou.im.entity.Userinfo
import com.jiayou.im.service.impl.RelationServiceImpl
import com.jiayou.im.service.impl.UserinfoServiceImpl
import com.jiayou.im.vo.Resoult
import org.apache.ibatis.annotations.Param
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


/**
 *
 *
 * 前端控制器
 *
 *
 * @author lishuang
 * @since 2020-11-20
 */
@RestController
@RequestMapping("/im/relation")
class RelationController {

    @Autowired
    private lateinit var relationServiceImpl: RelationServiceImpl

    @Autowired
    private lateinit var userInFoServiceImpl: UserinfoServiceImpl

    //获取好友列表
    @GetMapping("getRelation")
    fun getRelations(@Param("id") id: String): Resoult<ArrayList<Userinfo>> {
        val relationsList = relationServiceImpl.list(
            KtQueryWrapper(Relation::class.java).eq(Relation::id1, id).or().eq(Relation::id2, id)
        )
        val idList = ArrayList<Userinfo>()
        relationsList.forEach {
            val user = userInFoServiceImpl.getById(if (it.id1 == id) it.id2 else it.id1)
            if (user != null)
                idList.add(user)
        }

        return Resoult(200, idList)
    }

}
